package com.zlp.spring.init.design.observer;

import java.util.ArrayList;
import java.util.List;


/**
 * 主体操控被观察者对象
 * @date: 2022/1/17 17:37
 */
public class Subject {

   /**
    * 存放观察者对象集合
    */
   private List<Observer> observers = new ArrayList<Observer>();

   /**
    * 状态值
    */
   private int state;
 
   public int getState() {
      return state;
   }
 
   public void setState(int state) {
      this.state = state;
      notifyAllObservers();
   }
 
   public void attach(Observer observer){
      observers.add(observer);
   }


   /**
    * 通知所有观察员
    * @date: 2022/1/17 17:34
    * @return: void
    */
   public void notifyAllObservers(){
      for (Observer observer : observers) {
         observer.update();
      }
   }  
}